///////////////////////////////////////////////////
// Election Administration Polarization
// Make RD Generalization Plots
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"

* Estimate the slope of the conditional expectation function across different bandwidths
matrix define B0 = J(25, 5, .)
matrix define B1 = J(25, 5, .)
qui forval i=1/25 {
	forval t=0/1 {
		reg r_oos_state_year_vs_dem rv if abs(rv)<(`i'/100) & treat==`t', vce(clust election_id)
		matrix B`t'[`i',1] = _b[rv]
		matrix B`t'[`i',2] = _se[rv]
		matrix B`t'[`i',3] = _b[rv] - 1.96*_se[rv]
		matrix B`t'[`i',4] = _b[rv] + 1.96*_se[rv]
		matrix B`t'[`i',5] = e(N)
	}
}
clear
svmat B0 
svmat B1
rename (B*) (b0 se0 lower0 upper0 n0 b1 se1 lower1 upper1 n1)
gen bw = _n/100

* Plot how the slope of the conditional expectation function 
* changes across bandwidths in Rep counties
twoway (rcap lower0 upper0 bw, lc(gs4)) ///
	(scatter b0 bw, mc(gs2)) if bw>=0.05, ///
	ti("Rep Election Officials Only", c(black)) xti("Bandwidth") ///
	yti("Coef on Dem Elec Official Win Margin") ///
	graphregion(color(white)) legend(off)
graph export "$path/output/rd_generalization_reps.pdf", replace

* Plot how the slope of the conditional expectation function
* changes across bandwidths in Dem counties
twoway (rcap lower1 upper1 bw, lc(gs4)) ///
	(scatter b1 bw, mc(gs2)) if bw>=0.05, ///
	ti("Dem Election Officials Only", c(black)) xti("Bandwidth") ///
	yti("Coef on Dem Elec Official Win Margin") ///
	graphregion(color(white)) legend(off)
graph export "$path/output/rd_generalization_dems.pdf", replace
